<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="180">
      </el-table-column>



      <el-table-column prop="companyName" label="名称" width="100" sortable>
      </el-table-column>

      <el-table-column prop="companyNum" label="座机" width="120"  sortable>
      </el-table-column>

      <el-table-column prop="registerTime" label="注册时间" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="120"  sortable>

        <template slot-scope="scope">

          <span v-if="scope.row.state == 0" style="color:blue">待审核 </span>
          <span v-if="scope.row.state == 1">待激活</span>
          <span v-if="scope.row.state == 2" style="color:red">审核失败</span>
          <span v-if="scope.row.state == 3" style="color:green">正常</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="logo" label="logo" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="admin.username" label="管理员" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="120"  sortable>
      </el-table-column>

      <el-table-column label="操作" width="500">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="query.rows" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="saveTenantDate" :close-on-click-modal="false">
      <el-form :model="savaForm" label-width="80px" :rules="savaFormRules" ref="savaForm">
        <el-form-item label="公司名称" >
          <el-input v-model="savaForm.companyName" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="电话" >
          <el-input v-model="savaForm.companyNum" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" >
          <el-input v-model="savaForm.address" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="管理员" prop="manager">
          <el-select v-model="savaForm.admin" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in employees"
                :key="item.id"
                :label="item.username"
                :value="item">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="manager">
          <el-select v-model="savaForm.type" clearable value-key="id" placeholder="请选择">
            <!--下拉选项
              :key=""  //唯一标识
              :label 选择之后展示到选择框中的值
              :value 选中之后绑定给模型层的值  如果要绑定对象给模型层 有一个大坑
                 必须要写  value-key="id"
            -->
            <el-option
                v-for="item in tenantTypes"
                :key="item.id"
                :label="item.name"
                :value="item">
              <span style="float: left">{{ item.name }}</span>

            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="logo" >
          <el-input v-model="savaForm.logo" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveTenantDate = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


  </section>
</template>

<script>


export default {
  data() {
    return {
      filters: {
        name: ''
      },
      users: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列
      checkAll:false,
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      saveTenantDate: false,//新增界面是否显示
      addLoading: false,
      savaFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入标识', trigger: 'blur' }
        ]
      },
      //新增界面数据
      savaForm: {
        id:null,
        companyName:'',
        companyNum:'',
        adminId:null,
        typeId:null,
        address:'',
        logo:'',
        admin:{
          id:null,
          username:''
        },
        type:{
          id:null,
          name:''
        }
      },
      pageInfo:{
        rows:[],
        total:0
      },
      query:{
        rows:5,
        page:1,
        keyword:''
      },
      setPermissionVisible:false,
      permissionTree:[],
      tenantPermission:{
        permissionSns:[],
        tenantId:null
      },
      isIndeterminate:false,
      allPermissionSns:[],
      //菜单相关
      menusTree:[],
      setMenuVisible:false,
      tenantMenu:{
        menuName:[],
        tenantId:null,
        menuIds:[]
      },
      allMenus:[],
      employees:[],
      tenantTypes:[]

    }
  },
  methods: {
    //获取所有类型
    getTenantTypes(){
      this.$http.get("/sysmanage/tenantType")
          .then(result =>{
            result=result.data
            if(result.success){
              this.tenantTypes=result.resultObj;
            }

          })
    },

    //获取所有员工
    getEmployees(){
      this.$http.get("/sysmanage/employee")
          .then(result =>{
            result=result.data
            if(result.success){
                this.employees=result.resultObj;
            }

          })
    },

    //查询所有菜单
    getAllMenuName(){
      this.$http.get("/menu")
          .then(result=>{
            result=result.data;
            if(result.success){
              let array=result.object;
              for (let i = 0; i <array.length ; i++) {
                this.allMenus.push(array[i].id)
              }

            }

          })
          .catch(result=>{

          })
    },
    handleCheckAllMenu(val){

      this.tenantMenu.menuIds=val?this.allMenus:[]
      //console.log( this.tenantMenu.menuIds);

    },
    //页面刷新
    flushDate(){
      this.getTenants()
    },

    search(){
      this.query.currentPage=1;
      this.getTenants();
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getTenants();
    },
    //获取用户列表
    getTenants() {
      this.$http.post("/sysmanage/tenant/list",this.query)
          .then(result=>{
            result=result.data;
            if(result.success){
              this.pageInfo=result.resultObj;

            }
          })

    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {

        this.$http.delete("/tenant/"+row.id)
            .then(result =>{

              result=result.data
              if(result.success){
                this.$message({
                  message: '删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '删除失败'+result.message,
                  type: 'error'
                });
              }
              // //刷新页面
              this.search();
            })
      }).catch(() => {
        this.$message({
          message: '已取消',
          type: 'info'
        });
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveTenantDate = true;
      this.savaForm = Object.assign({}, row);

      this.getEmployees()
      this.getTenantTypes()
      this.savaForm.adminId= this.savaForm.admin?this.savaForm.admin.id:null
      this.savaForm.typeId=this.savaForm.type?this.savaForm.type.id:null
    },
    //显示新增界面
    handleAdd: function () {
      this.saveTenantDate = true;
      this.savaForm = {
        id:null,
        companyName:'',
        companyNum:'',
        adminId:null,
        typeId:null,
        address:'',
        logo:'',
        admin:{
          id:null,
          username:''
        },
        type:{
          id:null,
          name:''
        }
      };
      this.getEmployees()
      this.getTenantTypes()
    },
    //新增
    addSubmit: function () {
      this.savaForm.adminId= this.savaForm.admin?this.savaForm.admin.id:null
      this.savaForm.typeId=this.savaForm.type?this.savaForm.type.id:null
      this.$refs.savaForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {

            this.$http.put("/sysmanage/tenant",this.savaForm)
                .then(result =>{
                  this.listLoading=false;
                  result=result.data
                  if(result.success){
                    this.$message({
                      message: '保存成功',
                      type: 'success'
                    });
                  }else {
                    this.$message({
                      message: '保存失败'+result.message,
                      type: 'error'
                    });
                  }
                  //刷新页面
                  this.saveTenantDate=false;
                  this.search();
                })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;

        this.$http.patch("/tenant",ids)
            .then(result =>{
              this.listLoading=false;

              result=result.data
              if(result.success){
                this.$message({
                  message: '批量删除成功',
                  type: 'success'
                });
              }else {
                this.$message({
                  message: '批量删除失败'+result.message,
                  type: 'error'
                });
              }
              //刷新页面
              this.search();
            })


      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getTenants();
    // this.getAllMenuName()
  }
}

</script>

<style scoped>

</style>